package com.kgc.scd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kgc.scd.bean.Order;
import com.kgc.scd.bean.OrderVO;
import com.kgc.scd.mapper.OrderMapper;
import com.kgc.scd.service.OrderSerivce;
import com.kgc.scd.service.ProductService;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: WangKuoKuo
 * @create: 2022-06-10 14:31
 * @description: 订单中心
 **/

@Slf4j
@RestController
// @RequestMapping("/orderCenter")
@Api(tags = "订单中心")
public class OrderController {
    @Autowired
    private OrderSerivce orderSerivce;


    @GetMapping("/queryOrderList")
    @ApiOperation("查看订单")
    public RequestResult<List<OrderVO>> queryOrderList(@RequestParam("orderId") String orderId) {
        if (StringUtils.isEmpty(orderId)) {
            return ResultBuildUtil.fail("501", "订单号不存在");
        }
        List<OrderVO> orderVO = orderSerivce.queryOrderList(orderId);

        return ResultBuildUtil.success(orderVO);
    }

    @PostMapping("/delOderByOrderId")
    @ApiOperation("删除订单,order_state等于3才可以删")
    public RequestResult<Map<String, Object>> delOderByOrderId(String orderId) {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Order::getOrderId, orderId)
                .eq(Order::getOrderState, 3);

        if (orderSerivce.remove(lambdaQueryWrapper)) {
            map.put("code", "200");
            map.put("msg", orderId + "删除成功！");
            map.put("data", "");
            return ResultBuildUtil.success(map);
        }

        return ResultBuildUtil.fail("502", "订单不可以删除");

    }

    @GetMapping("/queryOderByOrderState")
    @ApiOperation("根据订单状态查询订单")
    @ApiImplicitParam(name = "orderState", value = " 订单状态(0:未支付 1:未发货 2:已发货 3：已签收)")
    public RequestResult<PageSupport<Order>> queryOderByOrderState(@RequestParam("userId") String userId,
                                                            @RequestParam(value = "orderState",required = false) Integer orderState,
                                                            @RequestParam("pageSize")Integer pageSize,
                                                            @RequestParam("pageNo")Integer pageNo,
                                                                   @RequestParam("order")Integer order) {
        if(orderState == null){
            PageSupport<Order> orders = orderSerivce.queryAllOderByuid(userId,pageSize,pageNo,order);
            return ResultBuildUtil.success(orders);
        }
        if(orderState > 3 || orderState < 0){
            return ResultBuildUtil.fail("503","订单状态错误！");
        }
        PageSupport<Order> orders = orderSerivce.queryOderByOrderState(userId,orderState,pageSize,pageNo,order);
        return ResultBuildUtil.success(orders);
    }


}
